﻿using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour 
{
    public Rigidbody Player;

    public float Speed;

    public float tilt;

    public Boundary _Boundary;

    public GameObject shot;
    public Transform shotSpawn;

    public float fireRate;
    private float nextFire;

    void Update()
    {
        if(Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
            AudioSource audio = GetComponent<AudioSource>();
            audio.Play();
        }
    }

    void FixedUpdate()
    {
        float MoveHorizontal = Input.GetAxis("Horizontal");
        float MoveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(MoveHorizontal, 0.0f, MoveVertical);

        Player.velocity = movement * Speed * Time.deltaTime;

        Player.position = new Vector3
        (
            Mathf.Clamp(Player.position.x, _Boundary.xMin, _Boundary.xMax), 
            0.0f,
            Mathf.Clamp(Player.position.z, _Boundary.zMin, _Boundary.zMax)
        );

        Player.rotation = Quaternion.Euler(0.0f, 0.0f, Player.velocity.x * tilt);
    }    
}
